unit PEvent;

interface

uses Forms, PChildPngForm, Windows, PEdtForm, Messages, SysUtils, Dialogs, Controls,
     Classes;

type
  TState = (visible, invisible, active);

  TParamEvent = record
    id: Integer;
    group: String;
    dateEvent: TDateTime;
    typeEvent: Byte;
    title: String;
    text: String;
  end;

  TEvent = class;

  TEdtTextForm  = class(TEdtForm);

  TEventPngForm = class(TChildPngForm)
    constructor Create(event: TEvent; AOwner: TObject; idComponent: Integer; dirImage: String; leftF, topF: Integer);
    procedure FormClick(Sender: TObject);
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    private
      titleEvent: TEdtTextForm;
      groupEvent: TEdtTextForm;
      dateEvent: TEdtTextForm;
    public
      procedure WMWindowPosChanged(var Msg: TWMWindowPosChanged); message WM_WINDOWPOSCHANGED;
  end;

  TEvent = class(TObject)
    constructor Create(parameters: TParamEvent);
  private
    id: Integer;
    group: String;
    dateEvent: TDateTime;
    typeEvent: Byte;
    title: String;
    text: String;
    position: Byte;
    state: TState;
    eventForm: TEventPngForm;
    imgNormal: HDC;
    imgActive: HDC;
  public
    function getId: Integer;
    function getGroup: String;
    function getDateEvent: TDateTime;
    function getTypeEvent: Byte;
    function getTitle: String;
    function getText: String;
    function getPosition: Byte;
    function getState: TState;
    function getEventForm:TEventPngForm;
    procedure changeState(stateF: TState);
    procedure changePosition(positionF: Byte);
    function createEventForm(number: Integer): TEventPngForm;
  end;

implementation

uses PParentPngForm, PMainForm;

{ TEvent }

procedure TEvent.changePosition(positionF: Byte);
begin
  position := positionF;
  //
end;

procedure TEvent.changeState(stateF: TState);
begin
  state := stateF;
  //
end;

constructor TEvent.Create(parameters: TParamEvent);
begin
  inherited Create;

  id := parameters.id;
  group := parameters.group;
  dateEvent := parameters.dateEvent;
  typeEvent := parameters.typeEvent;
  title := parameters.title;
  text := parameters.text;

  state := invisible;

end;

function TEvent.createEventForm(number: Integer): TEventPngForm;
var img: String;
begin
  if (typeEvent = 1) then
    img := 'Img/Background_Event_Informative_Normal.png'
  else
    if (typeEvent = 2) then
      img := 'Img/Background_Event_Typical_Normal.png'
    else
      img := 'Img/Background_Event_Important_Normal.png';

  eventForm := TEventPngForm.Create(Self, MainForm, number, img, 14, 37 + 55 * number + 1);
  Result := eventForm;
end;

function TEvent.getDateEvent: TDateTime;
begin
  Result := dateEvent;
end;

function TEvent.getEventForm: TEventPngForm;
begin
  Result := eventForm;
end;

function TEvent.getGroup: String;
begin
  Result := group;
end;

function TEvent.getId: Integer;
begin
  Result := id;
end;

function TEvent.getPosition: Byte;
begin
  Result := position;
end;

function TEvent.getState: TState;
begin
  Result := state;
end;

function TEvent.getText: String;
begin
  Result := text;
end;

function TEvent.getTitle: String;
begin
  Result := title;
end;

function TEvent.getTypeEvent: Byte;
begin
  Result := typeEvent;
end;


{ TEventPngForm }

constructor TEventPngForm.Create(event: TEvent; AOwner: TObject; idComponent: Integer; dirImage: String; leftF, topF: Integer);
begin
  inherited Create(AOwner, idComponent, dirImage, leftF, topF);
  Self.Show;

  titleEvent := TEdtTextForm.Create(Self, 0, '', 55, 18);
  titleEvent.setText(event.getTitle);
  titleEvent.Edit.Font.Size := 15;
  titleEvent.Edit.Height := 23;
  titleEvent.Edit.Enabled := False;
  titleEvent.Show;

  groupEvent := TEdtTextForm.Create(Self, 0, '', 190, 3);
  groupEvent.setText(event.getGroup);
  groupEvent.Edit.Font.Size := 8;
  groupEvent.Edit.Height := 11;
  groupEvent.Edit.Width := 45;
  groupEvent.Width := 46;
  groupEvent.Edit.Enabled := False;
  groupEvent.Show;

  dateEvent := TEdtTextForm.Create(Self, 0, '', 180, 40);
  dateEvent.setText(DateTimeToStr(event.getDateEvent));
  dateEvent.Edit.Font.Size := 8;
  dateEvent.Edit.Height := 11;
  dateEvent.Edit.Width := 55;
  dateEvent.Width := 56;
  dateEvent.Edit.Enabled := False;
  dateEvent.Show;

end;

procedure TEventPngForm.FormClick(Sender: TObject);
begin
//  titleEvent.Show;
end;

procedure TEventPngForm.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  titleEvent.Show;
  groupEvent.Show;
  dateEvent.Show;
end;

procedure TEventPngForm.WMWindowPosChanged(var Msg: TWMWindowPosChanged);
begin
  inherited;

  if (titleEvent <> nil) and (groupEvent <> nil) and (dateEvent <> nil) then
  begin
    titleEvent.BoundsRect := titleEvent.GetRect(Self, titleEvent.getXPos, titleEvent.getYPos, titleEvent.Left + titleEvent.Width, titleEvent.Top + titleEvent.Height);
    groupEvent.BoundsRect := groupEvent.GetRect(Self, groupEvent.getXPos, groupEvent.getYPos, groupEvent.Left + groupEvent.Width, groupEvent.Top + groupEvent.Height);
    dateEvent.BoundsRect := dateEvent.GetRect(Self, dateEvent.getXPos, dateEvent.getYPos, dateEvent.Left + dateEvent.Width, dateEvent.Top + dateEvent.Height);
  end;
end;

end.
